//
//  TiDanDetailVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "TiDanDetailVC.h"
#import "TiDanDetailModel.h"
#import "TiDanDetailCell.h"
#import "TiDanDetailHeadView.h"
#import "TiDanDetailMapVC.h"
#import "TiDanChooseCarVC.h"
#import "TiDanDetailCarCell.h"
#import "TiDanGoodListVC.h"

@interface TiDanDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) TiDanDetailHeadView *headView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TiDanDetailModel *model;
@property (nonatomic, strong) UIButton *jieDanBtn;
@property (nonatomic, strong) UIButton *mapBtn;

@end

@implementation TiDanDetailVC

-(TiDanDetailModel *)model {
    if (!_model) {
        _model = [TiDanDetailModel new];
    }
    return _model;
}

-(TiDanDetailHeadView *)headView {
    if (!_headView) {
        _headView = [[TiDanDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 80)];
    }
    return _headView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - NaviHeight - 64 - BottomHeight, ScreenW, 64 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        self.mapBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (ScreenW - 60 - 15) * 0.5, 44)];
        [self.mapBtn setTitleColor:HexColor(0x3D68EB) forState:0];
        self.mapBtn.titleLabel.font = BoldFontOfSize(16);
        [self.mapBtn setTitle:@"查看提货地图" forState:0];
        [self.mapBtn setImage:[UIImage imageNamed:@"icon_location_map"] forState:0];
        self.mapBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        self.mapBtn.backgroundColor = HexColorA(0x3D68EB, 0.1);
        self.mapBtn.layer.cornerRadius = 6;
        [self.mapBtn addTarget:self action:@selector(mapClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:self.mapBtn];
        
        self.jieDanBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.mapBtn.nim_right + 15, 10, (ScreenW - 60 - 15) * 0.5, 44)];
        [self.jieDanBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.jieDanBtn.titleLabel.font = BoldFontOfSize(16);
        [self.jieDanBtn setTitle:@"接单" forState:0];
        [self.jieDanBtn setImage:[UIImage imageNamed:@"icon_adopt_detail"] forState:0];
        self.jieDanBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        self.jieDanBtn.backgroundColor = Color_ThemeColor;
        self.jieDanBtn.layer.cornerRadius = 6;
        [self.jieDanBtn addTarget:self action:@selector(jieDanClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:self.jieDanBtn];
    }
    return _bottomView;
}

- (void)mapClick {
    TiDanDetailMapVC *vc = [TiDanDetailMapVC new];
    vc.list = self.model.list;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)jieDanClick {
    if ([self.model.info.dispatch isEqualToString:@"2"]) {
        //供应商调度，选择车辆
        TiDanChooseCarVC *vc = [TiDanChooseCarVC new];
        vc.sdl_id = self.sdl_id;
        vc.sdlv_id = self.sdlv_id;
        vc.isModify = NO;
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else {
        //直接调度
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定接单？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self requestForJieDan];
        }];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:sureAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)requestForJieDan {
    [HttpRequest PostWithApi:LogisticsSupplierTiDanManagerJieDan params:@{@"sdl_id":self.sdl_id,@"sdlv_id":self.sdlv_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"接单成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshTiDanShouLiList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headView.nim_bottom, ScreenW, ScreenH -  self.headView.nim_bottom - NaviHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self.view addSubview:self.headView];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierTiDanManagerDetail params:@{@"sdl_id":self.sdl_id,@"sdlv_id":self.sdlv_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [TiDanDetailModel mj_objectWithKeyValues:responseObject];
            self.model.info.dispatch = self.dispatch;
            self.headView.model = self.model;
            if ([self.model.info.is_accept isEqualToString:@"1"]) {
                [self.bottomView removeFromSuperview];
                self.tableView.nim_height = ScreenH - self.headView.nim_bottom - NaviHeight;
            }else {
                [self.view addSubview:self.bottomView];
                self.tableView.nim_height = ScreenH - self.bottomView.nim_height - self.headView.nim_bottom - NaviHeight;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self.model.info.is_accept isEqualToString:@"1"]) {
        return 2;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.model.info.is_accept isEqualToString:@"1"]) {
        if (section == 0) {
            return 1;
        }
        return self.model.list.count;
    }
    return self.model.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.model.info.is_accept isEqualToString:@"1"]) {
        if (indexPath.section == 0) {
            TiDanDetailCarCell *cell = [TiDanDetailCarCell TiDanDetailCarCellWithTableView:tableView];
            cell.carNum = self.model.info.vehicle_num;
            cell.driver = self.model.info.driver_name;
            cell.canModify = [self.model.info.dispatch isEqualToString:@"2"];
            __weak_self;
            cell.modifyBlock = ^{
                TiDanChooseCarVC *vc = [TiDanChooseCarVC new];
                vc.sdl_id = weakself.sdl_id;
                vc.sdlv_id = weakself.sdlv_id;
                vc.isModify = YES;
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            };
            return cell;
        }
    }
    TiDanDetailCell *cell = [TiDanDetailCell TiDanDetailCellWithTableView:tableView];
    cell.model = self.model.list[indexPath.row];
    __weak_self;
    cell.listBlock = ^{
        TiDanGoodListVC *vc = [TiDanGoodListVC new];
        vc.dp_id = weakself.model.list[indexPath.row].dp_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.model.info.is_accept isEqualToString:@"1"]) {
        if (indexPath.section == 0) {
            TiDanDetailCarCell *cell = [TiDanDetailCarCell TiDanDetailCarCellWithTableView:tableView];
            return cell.height;
        }
    }
    TiDanDetailCell *cell = [TiDanDetailCell TiDanDetailCellWithTableView:tableView];
    cell.model = self.model.list[indexPath.row];
    return cell.height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
